package com.mzx.javase.io;

import javax.print.attribute.standard.Fidelity;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

/**
 * 类描述：
 * 文件复制
 * @ClassName FileCopy
 * @Description TODO
 * @Author 马子鑫
 * @Date 2022/7/20 10:41
 * @Version 1.0
 */
public class FileCopy {
    public static void main(String[] args) {
        FileInputStream fis = null;
        FileOutputStream fos = null;
        try {
            //文件输入流，源文件地址
            fis = new FileInputStream("D:\\软件安装包\\jdk-8u333-windows-x64.exe");
            //文件输出流，目的文件地址。目的文件不存在时，会自动创建
            fos = new FileOutputStream("D:\\java\\tempdirectory\\jdk8.exe");
            //创建一个字节数组，长度为1024字节，就是1kb，*10意思是10kb，每次复制10kb的数据
//            byte[] bytes = new byte[1024 * 10];
            long startTime = System.currentTimeMillis();
            byte[] bytes = new byte[1024];
            int length = 0;
            while ((length = fis.read(bytes)) != -1) {
                fos.write(bytes,0,length);
            }
            long endTime = System.currentTimeMillis();
            System.out.println((endTime-startTime) + "毫秒");
            fos.flush();
        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            if (fis != null) {
                try {
                    fis.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
            if (fos != null) {
                try {
                    fos.close();
                } catch (IOException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
